import http from '@/utils/http/index'

interface LoginAccount {
  username: string;
  password: string;
}

interface LoginSuccess {
  username: string;
  roles: string[];
  token: string;
  expires: string
}

// 用户登录
export const userLogin = (data: LoginAccount): Promise<LoginSuccess> => {
  return http.post('/sys/login', data)
}


export interface PageMenuNode {
  id: string
  path: string;
  name: string;
  component: string;
  redirect: string;
  meta: NodeMeta;
  children: PageMenuNode[];
}

export interface NodeMeta {
  icon: string;
  title: string;
  keepAlive: boolean;
  showLink: boolean;
  rank: number;
  roles: string[];
}

// 获取路由
export const getPageMenu = (): Promise<PageMenuNode[]> => {
  return http.get('sys/user/pageMenu')
}

// 用户的相关操作

export interface AddUserForm {
  username: string
  password: string
  status: number
  accountLv: number
  orgId: string
  nickname: string
  email: string
  cityCode: string
  roleIds: string[]
}
export interface UpdateUserInfoForm {
  userId: string
  status: number
  accountLv: number
  orgId: string
  nickname: string
  email: string
  cityCode: string
}

interface UserExtend {
  nickname: string
  email: string
  cityCode: string
}
export interface UserDto {
  userId: string
  username: string
  password: string
  status: number
  isOnline: number
  accountLv: number
  orgId: string
  extend: UserExtend
  creator: string
  creatorId: string
  createdAt: string
  orgName: string
}

export const addUser = (data: AddUserForm) => {
  return http.post('sys/user/add', data)
}
export const updateUser = (data: UpdateUserInfoForm) => {
  return http.put('sys/user/update', data)
}

export const updatePWD = (oldPassword: string, newPassword: string) => {
  return http.put('sys/user/update', { oldPassword, newPassword })
}

export const resetPWD = (id: string) => {
  return http.put('sys/user/pwd/' + id)
}
export const deleteUser = (id: string) => {
  return http.delete('sys/user/delete/' + id)
}
export const getUserList = (): Promise<UserDto[]> => {
  return http.get('sys/user/list')
}
export interface SearchUser {
  username: string
  orgId: string
  pageNum: number
  pageSize: number
}
interface UserDtoPage {
  list: UserDto[];
  total: number
}
export const getUserPage = (params: SearchUser): Promise<UserDtoPage> => {
  return http.get('sys/user/page', { params })
}

export interface UserBindOrg {
  userId: string
  orgIds: string[]
}
export const getUserBoundOrg = (id: string): Promise<string[]> => {
  return http.get('sys/user/boundOrg/' + id)
}

export const userbindOrg = (data: UserBindOrg) => {
  return http.put('sys/user/bindOrg', data)
}
export interface UserBindRole {
  userId: string
  roleIds: string[]
}
export const getUserBoundRole = (id: string): Promise<string[]> => {
  return http.get('sys/user/boundRole/' + id)
}
export const userbindRole = (data: UserBindRole) => {
  return http.put('sys/user/bindRole', data)
}

// 部门
export interface Org {
  id: string
  parentId: string
  orgName: string
  status: number
  creator: string
  createdAt: string
}
export interface OrgNode {
  id: string
  orgName: string
  parentId: string
  mate: Org
  label: string
  value: string
  children: OrgNode[]
}
export interface AddOrg {
  parentId: string
  orgName: string
  status: number
}
export const getOrgList = (): Promise<Org[]> => {
  return http.get('sys/org/listDto')
}
export const getOrgTree = (): Promise<OrgNode[]> => {
  return http.get('sys/org/tree')
}
export const addOrg = (data: AddOrg) => {
  return http.post('sys/org/create/', data)
}
export const delOrg = (id: string) => {
  return http.delete('sys/org/' + id)
}

// 角色

export interface Role {
  roleId: string
  roleName: string
  roleCode: string
  roleDesc: string
  isSys: number
  isOpen: number
  creator: string
  createdAt: string
}
export interface AddRoleForm {
  roleName: string
  roleCode: string
  roleDesc: string
  isOpen: number
}
export const getRoleList = (): Promise<Role[]> => {
  return http.get('sys/role/list')
}
export const addRole = (data: AddRoleForm) => {
  return http.post('sys/role/add', data)
}
export const delRole = (id: string) => {
  return http.delete('sys/role/' + id)
}
